package cn.dujc.superkid.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.URLImageParser;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.QuestionResultsResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;

/**
 * Created by Administrator on 2018/10/22 0022.
 */

public class ErrorContinueActivity extends BaseActivity implements View.OnClickListener{
    private TextView continueTitle;
    private RadioGroup radioGroup;
    private View checksGroup;
    EditText edit;
    private AppCompatCheckBox checkBox1, checkBox2, checkBox3, checkBox4;
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;
    private String type;
    private String title;
    private String answer;
    private List<String> answers;
    private String good_key;
    private String action;
    @Override
    public int getViewId() {
        return R.layout.activity_continue;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        setTitle("继续答题");
        try {
            initData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        initViews();
        initType();

    }

    private void initType() {
        switch (type) {
            case "1": //单选
                edit.setVisibility(View.GONE);
                checksGroup.setVisibility(View.GONE);
                break;
            case "2": //多选
                edit.setVisibility(View.GONE);
                radioGroup.setVisibility(View.GONE);
                break;
            case "3": //填空
                radioGroup.setVisibility(View.GONE);
                checksGroup.setVisibility(View.GONE);
                break;
        }
    }

    private void initData() throws JSONException {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        title = intent.getStringExtra("title");
        action=intent.getStringExtra("action");
        good_key = intent.getStringExtra("good_key");
        if (type.equals("2")) {
            answers = intent.getStringArrayListExtra("answer");
//            JSONArray jsonArray = new JSONArray(answer);
//            answers = new ArrayList<>();
//            if (jsonArray.length() > 0) {
//                answers.add(jsonArray.getString(0));
//                answers.add(jsonArray.getString(1));
//                answers.add(jsonArray.getString(2));
//                answers.add(jsonArray.getString(3));
//            }
        } else if (type.equals("1")) {
            answers = intent.getStringArrayListExtra("answer");
        }

    }

    private void initViews() {
        findViewById(R.id.continue_submit).setOnClickListener(this);
        continueTitle = findViewById(R.id.continue_title);
        URLImageParser imageGetter = new URLImageParser(continueTitle);
        continueTitle.setText(Html.fromHtml(title, imageGetter, null));

        checksGroup = findViewById(R.id.cotinue_checks_group);
        radioGroup = findViewById(R.id.cotinue_radio_group);

        radioButton1 = findViewById(R.id.cotinue_radio_a);
        radioButton2 = findViewById(R.id.cotinue_radio_b);
        radioButton3 = findViewById(R.id.cotinue_radio_c);
        radioButton4 = findViewById(R.id.cotinue_radio_d);

        checkBox1 = findViewById(R.id.continue_selects_a);
        checkBox2 = findViewById(R.id.continue_selects_b);
        checkBox3 = findViewById(R.id.continue_selects_c);
        checkBox4 = findViewById(R.id.continue_selects_d);
        edit = findViewById(R.id.cotinue_text_edit);
        setSelectData();
    }

    private void setSelectData() {
        switch (type) {
            case "1":
                radioButton1.setText("A：" + answers.get(0));
                radioButton2.setText("B：" + answers.get(1));
                radioButton3.setText("C：" + answers.get(2));
                radioButton4.setText("D：" + answers.get(3));
                break;
            case "2":
                checkBox1.setText("A：" + answers.get(0));
                checkBox2.setText("B：" + answers.get(1));
                checkBox3.setText("C：" + answers.get(2));
                checkBox4.setText("D：" + answers.get(3));
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.continue_submit) {
            checkSubmit();
        }
    }

    String values;

    private void checkSubmit() {
        switch (type) {
            case "1":
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.cotinue_radio_a:
                        values = "{\"answer1\":[\"1\"]}";
                        break;
                    case R.id.cotinue_radio_b:
                        values = "{\"answer1\":[\"2\"]}";
                        break;
                    case R.id.cotinue_radio_c:
                        values = "{\"answer1\":[\"3\"]}";
                        break;
                    case R.id.cotinue_radio_d:
                        values = "{\"answer1\":[\"4\"]}";
                        break;
                    default:
                        ToastUtil.showToast(this, "请选择答案");
                        return;
                }
                break;
            case "2":
//                setChecksData();
                String v = "";
                if (checkBox1.isChecked()) {
                    v = v + ",\"1\"";
                }
                if (checkBox2.isChecked()) {
                    v = v + ",\"2\"";
                }
                if (checkBox3.isChecked()) {
                    v = v + ",\"3\"";
                }
                if (checkBox4.isChecked()) {
                    v = v + ",\"4\"";
                }
                if (!v.equals("")) {
//                    "answer": "{\"answer1\":[\"3\"]}",
                    values = "\"answer1\"" + ":[" + v.substring(1) + "]";
                } else {
                    ToastUtil.showToast(this, "请填写答案");
                    return;
                }
                break;
            case "3":
                if (TextUtils.isEmpty(edit.getText())) {
                    ToastUtil.showToast(this, "请填写答案");
                    return;
                }
                values = "\"answer1\"" + ":\"" + edit.getText() + "\"";
                break;
        }
        setChecksData();
    }

    private void setChecksData() {
        RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        QMap map=QMap.build()
                .put("answer", "{" + values + "}")
                .put("wrong_key", good_key)
                .put("user_id", Integer.parseInt(loginResult.getUid()))
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode()).create();
        Log.e("qqmap",map.toString());
        HttpHelper.getUserCenter().memberWrongQuestionResults(map).enqueue(new ResultCallBack<Result<QuestionResultsResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<QuestionResultsResult> body) {
                ToastUtil.showToast(mActivity,body.getData().getInfo());
                sendBroadcast(new Intent(action));
                finish();
            }
        });

    }
}
